import sys
import multiprocessing
from time import sleep
from datetime import datetime


def delay_test(n):
    """验证多进程的任务是提前分配好还是运行时临时分配：结论是运行时临时分配，跑完任务的进程接着领下一个任务"""
    if n % 3 == 0:
        print(n, "sleeping 5")
        sleep(20)
        print(n, "done")
    else:
        print(n, "sleep 1")
        sleep(1)


if __name__ == '__main__':
    pool = multiprocessing.Pool(3)
    for a in range(10):
        print(a, datetime.now())
        r = pool.apply_async(delay_test, args=(a,))
    pool.close()
    pool.join()
